import { ElMessage, ElMessageBox } from 'element-plus';
export function getUrl() {
	return import.meta.env.VITE_API_URL
}

export function uuid(len:any,radix:any) {
	let chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
		let uuid = [];
		radix = radix || chars.length;
		if (len) {
			// 如果指定uuid长度,只是取随机的字符,0|x为位运算,能去掉x的小数位,返回整数位
			for (let i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix];
		} else {
			let r;
			// rfc4122标准要求返回的uuid中,某些位为固定的字符
			uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
			uuid[14] = '4';
			for (let i = 0; i < 36; i++) {
				if (!uuid[i]) {
					r = 0 | Math.random() * 16;
					uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
				}
			}
	}
	return uuid.join('');
}

//删除对象里为空属性
export function deleteParams(row: Object, rule: any[] = []) {
	let paramsKeys = Object.keys(row);
	let paramsValues = Object.values(row);
	let params = deepCopy(row);

	paramsValues.forEach((item: any, index: number) => {
		if ((item === null || item === '' || item === undefined) && rule.indexOf(paramsKeys[index]) === -1) {
			delete params[paramsKeys[index]];
		}
	});
	return params;
}
//校验对象里面是否有空的
export function ruleParams(row: Object, rules: any[] = [], ruleType: number = 0, msg: string) {
	return new Promise((resolve: any, reject: any) => {
		let flag = false;
		let errorKey = <any>[]; //有问题的key值
		let keys: any = Object.keys(row);
		let values: any = Object.values(row);
		switch (ruleType) {
			case 0: //校验对象 过滤rules字段
				rules = rules.concat(['remark', 'sort', 'delFlag']);
				if (rules && rules.length > 0) {
					//过滤规则数组
					values.forEach((item: any, index: number) => {
						// console.log(keys[index],rules.indexOf(keys[index]) === -1);
						if ((item === null || item === '' || item === undefined || item.length === 0) && rules.indexOf(keys[index]) === -1) {
							errorKey.push(keys[index]);
							flag = true;
							
						}
					});
				} else {
					values.forEach((item: any, index: number) => {
						if (!item) {
							errorKey.push(keys[index]);
							flag = true;
						}
					});
				}
				break;
			case 1: //校验对象 只校验单个字段 rules是校验关键key
				values.forEach((item: any, index: number) => {
					if ((item === null || item === '' || item === undefined) && keys[index] === rules) {
						errorKey.push(keys[index]);
						flag = true;
					}
				});
				break;
			case 2: //校验对象 只校验rules内存在的字段
				values.forEach((item: any, index: number) => {
					if ((item === null || item === '' || item === undefined) && rules.indexOf(keys[index]) !== -1) {
						errorKey.push(keys[index]);
						flag = true;
					}
				});
				break;
		}
		
		if (flag) {
			ElMessage({
				message: msg ? msg : '请补全信息',
				grouping: true,
				type: 'warning',
			});
			
			reject(errorKey);
		} else {
			resolve();
		}
	});
}
//深拷贝
export function deepCopy(row: any) {
	if (typeof row !== 'object' || row === null) {
		return row;
	} // 结果对象
	let newObj: any = {}; // 如果是数组，则定义结果数组
	if (row.constructor === Array) {
		newObj = [];
	} // 遍历对象的key
	for (const key in row) {
		// key是对象的自有属性，递归调用深拷贝方法
		if (row.hasOwnProperty(key)) {
			newObj[key] = deepCopy(row[key]);
		}
	}
	return newObj;
}
//初始化对象赋值
export function initRow(row: any, params: any) {
	if (params) {
		let rowKeys = Object.keys(row);
		let paramsKeys = Object.keys(params);
		let data = <any>{};
		console.log(paramsKeys);
		rowKeys.forEach((item: any, index: number) => {
			if (paramsKeys.indexOf(item) > -1) {
				data[item] = params[item];
			}
		});

		return data;
	} else {
		return row;
	}
}
//对象里面数组转字符串
export function arrayToString(row: Object) {
	let paramsKeys = Object.keys(row);
	let paramsValues = Object.values(row);
	let params = deepCopy(row);
	paramsValues.forEach((item: any, index: number) => {
		if (Array.isArray(item)) {
			params[paramsKeys[index]] = item.toString();
		}
	});
	return params;
}

//对象里面符合条件的转为数组
export function stringToArray(row: any, rule = [], split = ',') {
	let paramsKeys = Object.keys(row);
	let paramsValues = Object.values(row);
	let params = deepCopy(row);
	paramsKeys.forEach((item: any, index: number) => {
		if (rule.indexOf(item) > -1 && params[paramsKeys[index]]) {
			params[paramsKeys[index]] = params[paramsKeys[index]].split(split);
		}
	});
	return params;
}

export function formatPage(dataPool: any[], pageData: any[]) {
	if (dataPool && dataPool.length > 0) {
		return dataPool.concat(pageData);
	} else {
		return pageData;
	}
}
/**
	 * @description JS对象深度合并
	 * @param {object} target 需要拷贝的对象
	 * @param {object} source 拷贝的来源对象
	 * @returns {object|boolean} 深度合并后的对象或者false（入参有不是对象）
	 */
	export function deepMerge(target: any, source: any) {
		target = deepCopy(target);
		if (typeof target !== 'object' || typeof source !== 'object') return false;
		for (const prop in source) {
			if (!source.hasOwnProperty(prop)) continue;
			if (prop in target) {
				if (typeof target[prop] !== 'object') {
					target[prop] = source[prop];
				} else if (typeof source[prop] !== 'object') {
					target[prop] = source[prop];
				} else if (target[prop].concat && source[prop].concat) {
					target[prop] = target[prop].concat(source[prop]);
				} else {
					target[prop] = deepMerge(target[prop], source[prop]);
				}
			} else {
				target[prop] = source[prop];
			}
		}
		return target;
	}
// 生成全局唯一标识符
export function guid(len: number = 32, firstU: boolean = true, radix: any = null) {
	let chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
	let uuid = [];
	radix = radix || chars.length;
	if (len) {
		// 如果指定uuid长度,只是取随机的字符,0|x为位运算,能去掉x的小数位,返回整数位
		for (let i = 0; i < len; i++) uuid[i] = chars[0 | (Math.random() * radix)];
	} else {
		let r;
		// rfc4122标准要求返回的uuid中,某些位为固定的字符
		uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
		uuid[14] = '4';
		for (let i = 0; i < 36; i++) {
			if (!uuid[i]) {
				r = 0 | (Math.random() * 16);
				uuid[i] = chars[i == 19 ? (r & 0x3) | 0x8 : r];
			}
		}
	}
	// 移除第一个字符,并用u替代,因为第一个字符为数值时,该guuid不能用作id或者class
	if (firstU) {
		uuid.shift();
		return 'u' + uuid.join('');
	} else {
		return uuid.join('');
	}
}
let timer: any;
let flag: any;
/**
 * 节流原理：在一定时间内，只能触发一次
 *
 * @param {Function} func 要执行的回调函数
 * @param {Number} wait 延时的时间
 * @param {Boolean} immediate 是否立即执行
 * @return null
 */
export function throttle(func: Function, wait = 500, immediate = false) {
	if (immediate) {
		if (!flag) {
			flag = true;
			// 如果是立即执行，则在wait毫秒内开始时执行
			typeof func === 'function' && func();
			timer = setTimeout(() => {
				flag = false;
			}, wait);
		}
	} else if (!flag) {
		flag = true;
		// 如果是非立即执行，则在wait毫秒内的结束处执行
		timer = setTimeout(() => {
			flag = false;
			typeof func === 'function' && func();
		}, wait);
	}
}
// 判断arr是否为一个数组，返回一个bool值
export function isArray (arr:any) {
    return Object.prototype.toString.call(arr) === '[object Array]';
}

// 深度克隆
export function deepClone (obj:any) {
 // 对常见的“非”值，直接返回原来值
 if([null, undefined, NaN, false].includes(obj)) return obj;
    if(typeof obj !== "object" && typeof obj !== 'function') {
  //原始类型直接返回
        return obj;
    }
    var o = isArray(obj) ? [] : {};
    for(let i in obj) {
        if(obj.hasOwnProperty(i)){
            o[i] = typeof obj[i] === "object" ? deepClone(obj[i]) : obj[i];
        }
    }
    return o;
}
